package com.miaosha.demo.controller;


import com.miaosha.demo.pojo.User;
import com.miaosha.demo.service.IGoodsService;
import com.miaosha.demo.service.IUserService;
import com.miaosha.demo.vo.DetailVo;
import com.miaosha.demo.vo.GoodsVo;
import com.miaosha.demo.vo.RespBean;
import com.sun.org.apache.xpath.internal.operations.Mod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.util.StringUtils;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IUserService userService;
    @Autowired
    private RedisTemplate<String,Object>redisTemplate;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;

    /*
    优化前qps:370
    缓存qps:3160

    */
    @RequestMapping(value="/toList",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toList(Model model,User user,
                         HttpServletRequest request,
                         HttpServletResponse response){
        ValueOperations<String, Object> valueOperations =
                redisTemplate.opsForValue();
        String html = (String) valueOperations.get("goodsList");
        if(!StringUtils.isEmpty(html)){
            return html;
        }
        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsService.findGoodsVo());
        //return "goodsList";

        WebContext webContext=new WebContext(request,response,
                request.getServletContext(),
                request.getLocale(),
                model.asMap());
        html = thymeleafViewResolver.getTemplateEngine()
                .process("goodsList", webContext);
        if(!StringUtils.isEmpty(html)){
            valueOperations.set("goodsList",html,60, TimeUnit.SECONDS);
        }
        return html;
    }
    @RequestMapping(value="/toDetail2",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toDetail2(Model model,User user,long goodsId,
                           HttpServletRequest request,
                           HttpServletResponse response){
        ValueOperations<String, Object> valueOperations =
                redisTemplate.opsForValue();
        String html =
                (String) valueOperations.get("goodsDetail:" + goodsId);
        if(!StringUtils.isEmpty(html))
            return html;
        model.addAttribute("user",user);
        GoodsVo goodsvo =
                goodsService.findGoodsByGoodsId(goodsId);
        Date startDate=goodsvo.getStartDate();
        Date endDate=goodsvo.getEndDate();
        Date nowDate=new Date();
        int secKillStatus=0;
        int remainSeconds=0;
        if(nowDate.before(startDate))
        {
            remainSeconds=(int)((startDate.getTime()-nowDate.getTime())/1000);
        }else if(nowDate.after(endDate))
        {
            secKillStatus=2;
            remainSeconds=-1;
        }else {
            secKillStatus=1;
        }
        model.addAttribute("goods",goodsvo);
        model.addAttribute("secKillStatus",secKillStatus);
        model.addAttribute("remainSeconds",remainSeconds);

        WebContext webContext = new WebContext(request, response,
                request.getServletContext(),
                request.getLocale(),
                model.asMap());
        html = thymeleafViewResolver.getTemplateEngine()
                .process("goodsDetail",webContext);
        //return "goodsDetail";
        if(!StringUtils.isEmpty(html))
            valueOperations.set("goodsDetail:"+goodsId,
                    html,
                    60,
                    TimeUnit.SECONDS);
        return html;
    }

    @RequestMapping("/detail")
    @ResponseBody
    public RespBean toDetail(Model model, User user, long goodsId){
        GoodsVo goodsvo =
                goodsService.findGoodsByGoodsId(goodsId);
        Date startDate=goodsvo.getStartDate();
        Date endDate=goodsvo.getEndDate();
        Date nowDate=new Date();
        int secKillStatus=0;
        int remainSeconds=0;
        if(nowDate.before(startDate))
        {
            remainSeconds=(int)((startDate.getTime()-nowDate.getTime())/1000);
        }else if(nowDate.after(endDate))
        {
            secKillStatus=2;
            remainSeconds=-1;
        }else {
            secKillStatus=1;
        }
        DetailVo detailVo=new DetailVo();
        detailVo.setUser(user);
        detailVo.setGoodsVo(goodsvo);
        detailVo.setSecKillStatus(secKillStatus);
        detailVo.setRemainSeconds(remainSeconds);
        return RespBean.success(detailVo);
    }
}
